package com.ctshk.rpc.order.tour.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.tour.code.IsDeletedCode;
import com.ctshk.rpc.order.tour.code.RefundStatusCode;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristRefundAddReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristRefundListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import java.util.Set;

/**
 * <p>
 * 系列团游客签证记录 Provider 接口
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
public class SeriesOrderTouristRefundProvider {

    public String selectListBySelective(IPage<SeriesOrderTouristRefundListReq> pageParam,
                                        @Param("req") SeriesOrderTouristRefundListReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsoti.order_id, tsoti.schedule_id, tsoti.id as tourist_id, " +
                "tsoti.cn_surname, tsoti.cn_name,tsoti.en_surname, tsoti.en_name, " +
                "tsotr.id, tsotr.refund_status, tsoti.total_paid_in, ");
        sb.append(
                "( select sum( temp3.actual_refund_amount ) FROM t_series_order_tourist_refund temp4 " +
                        "LEFT JOIN t_series_order_tourist_refund_detail temp3 " +
                        "ON temp4.id = temp3.refund_id where temp4.refund_status in (" +
                        RefundStatusCode.APPROVAL.getCode()
                        + ", " + RefundStatusCode.APPROVAL_PASS.getCode()
                        + ", " + RefundStatusCode.ALREADY.getCode() + ") and temp4.tourist_id = tsoti.id) as actual_refund_amount, ");
        sb.append(
                "( select sum( temp2.refund_amount ) FROM t_series_order_tourist_refund temp1 " +
                        "LEFT JOIN t_series_order_tourist_refund_detail temp2 " +
                        "ON temp1.id = temp2.refund_id where temp1.refund_status in (" +
                        RefundStatusCode.APPROVAL.getCode()
                        + ", " + RefundStatusCode.APPROVAL_PASS.getCode()
                        + ", " + RefundStatusCode.ALREADY.getCode() + ") and temp1.tourist_id = tsoti.id) as refund_amount ");
        sb.append("FROM t_series_order_tourist_info tsoti ");
        sb.append("LEFT JOIN ( SELECT temp.id, temp.tourist_id, temp.refund_status, temp.received_amount, temp.refund_amount, temp.gmt_create, temp.is_deleted " +
                "FROM t_series_order_tourist_refund temp where temp.is_deleted = 0 ORDER BY temp.gmt_create desc LIMIT 1 ) tsotr ON tsotr.tourist_id = tsoti.id ");
        sb.append("where tsoti.is_full_info = 1 ");
        sb.append("and tsoti.order_id = #{req.orderId} ");
        sb.append("and tsoti.is_deleted=" + IsDeletedCode.NO.getCode() + " ");
        sb.append("and (tsotr.is_deleted=" + IsDeletedCode.NO.getCode() + " or tsotr.is_deleted is null )");
        if (StringUtils.isNotBlank(req.getCondition1())) {
            sb.append("and ((tsoti.cn_surname || tsoti.cn_name) like concat('%',#{req.condition1},'%') " +
                    "OR (tsoti.en_name || tsoti.en_surname) like concat('%',#{req.condition1},'%')) ");
        }
        sb.append("order by tsotr.gmt_create desc ");
        return sb.toString();
    }

    public String selectAddByRefundDetailTypeId(@Param("req") SeriesOrderTouristRefundAddReq req,
                                                @Param("param1") Set<Long> param1) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsotrd.refund_id, tsotrd.refund_detail_type_id ");
        sb.append("FROM t_series_order_tourist_refund_detail tsotrd ");
        sb.append("LEFT JOIN t_series_order_tourist_refund tsotr ON tsotr.id = tsotrd.refund_id ");
        sb.append("where 1=1 ");
        sb.append("and tsotr.order_id = #{req.orderId} ");
        sb.append("and tsotr.tourist_id = #{req.touristId} ");
        if (param1.size() > 0) {
            sb.append(" and tsotrd.refund_detail_type_id in ( ");
            StringBuilder temp = new StringBuilder();
            int i = 0;
            for (Long id : param1) {
                i++;
                temp.append(id);
                if (i < param1.size()) {
                    temp.append(",");
                }
            }
            sb.append(temp);
            sb.append(" ) ");
        }
        return sb.toString();
    }

}
